package com.example.animationmodule

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.PointF
import android.graphics.drawable.AnimationDrawable
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowMetrics
import android.widget.Button
import android.widget.ImageView
import androidx.annotation.RequiresApi

class MainActivity : AppCompatActivity() {
	private lateinit var imageView: ImageView
	private lateinit var background: AnimationDrawable
	private lateinit var startTween: Button
	private lateinit var startActivity:Button
	private var width: Int = 0
	private var height: Int = 0
	private lateinit var huadong:Button

	@RequiresApi(Build.VERSION_CODES.R)
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_main)
		this.imageView = findViewById(R.id.imageView)
		this.imageView.setBackgroundResource(R.drawable.killsanpang)
		this.startTween = findViewById(R.id.btn_main_start_tween)
		this.startActivity=findViewById(R.id.btn_main_activity)
		this.huadong=findViewById(R.id.btn_main_huadong)

		this.startActivity.setOnClickListener {
			startActivity(Intent(this,SecondActivity::class.java))
			overridePendingTransition(R.anim.anim_translate_enter,R.anim.anim_translate_exit)
		}
		width = 500
		height = 500
	}

	fun start(view: View) {

		background = this.imageView.background as AnimationDrawable
		background.start()
	}

	fun stop(view: View) {
		background = this.imageView.background as AnimationDrawable
		if (background.isRunning) {
			background.stop()
		}
	}

	fun startTween(view: View) {
		startActivity(Intent(this, TweenActivity::class.java))
		overridePendingTransition(R.anim.set_anim_inter,R.anim.set_anim_inter)
	}

	fun startTweenXml(view: View) {
		startActivity(Intent(this, TweenActivityXml::class.java))
	}

	fun startInterpolator(view: View) {
		startActivity(Intent(this, InterpolatorActivity::class.java))
	}

	fun startSanpangCode(view: View) {
		val animationDrawable = AnimationDrawable()
		animationDrawable.addFrame(resources.getDrawable(R.drawable.img0), 500)
		animationDrawable.addFrame(resources.getDrawable(R.drawable.img3), 500)
		this.imageView.background = animationDrawable
		animationDrawable.start()

	}

	fun stopSanpangCode(view: View) {}

	/**
	 * 下面是属性动画的代码
	 */
	fun startTranslation(view: View) {
		val ofFloat = ObjectAnimator.ofFloat(view, "translationX",
				0f, 10f, 20f, 50f, 100f, 300f)
		ofFloat.duration = 5000
		ofFloat.start()
	}

	@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
	fun startRotate(view: View) {
		val ofFloat = ObjectAnimator.ofFloat(view, "rotation", 0f, 100f, 300f)
		ofFloat.duration = 3000
		ofFloat.setAutoCancel(true)
//        ofFloat.
		ofFloat.start()
	}

	fun startScale(view: View) {
		val ofFloat = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.7f, 0f)
		val ofFloat1 = PropertyValuesHolder.ofFloat("scaleX", 0f, 1f)
		val ofPropertyValuesHolder = ObjectAnimator.ofPropertyValuesHolder(view, ofFloat, ofFloat1)
		ofPropertyValuesHolder.duration = 2000
		ofPropertyValuesHolder.start()
//        ofPropertyValuesHolder.
	}

	fun startAlpha(view: View) {}

	//属性动画组合
	fun startObjectAnimatorSet(view: View) {
		val ofFloat = PropertyValuesHolder.ofFloat("alpha", 0f, 1f)
		val ofFloat1 = PropertyValuesHolder.ofFloat("scaleX", 0f, 1f)
		val ofFloat2 = PropertyValuesHolder.ofFloat("scaleY", 0f, 1f)
		val ofPropertyValuesHolder = ObjectAnimator.ofPropertyValuesHolder(view, ofFloat, ofFloat1, ofFloat2)
		ofPropertyValuesHolder.duration = 2000
		ofPropertyValuesHolder.start()
	}

	fun startUsr(view: View) {
		val valueAnimator = ValueAnimator()
		valueAnimator.setObjectValues(PointF(0f,0f))
		valueAnimator.duration = 2000
		//设置属性动画的执行过程
		//fraction: s/duration
		valueAnimator.setEvaluator { fraction, startValue, endValue ->
			val pointF = PointF()
			pointF.x = fraction * width
			pointF.y = Math.sqrt(pointF.x * width.toDouble()).toFloat()
			pointF
		}

		valueAnimator.addUpdateListener {
			//将当前位置点取出
			val animatedValue = valueAnimator.getAnimatedValue() as PointF
			imageView.translationX = animatedValue.x
			imageView.translationY = animatedValue.y

		}

		valueAnimator.start()
	}

	fun startSaomiao(view: View) {
		startActivity(Intent(this,SaomiaoActivity::class.java))
	}


}